package com.tal.live.moudels.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tal.live.annotation.ApiVersion;
import com.tal.live.annotation.ApiVersionConstant;
import com.tal.live.moudels.sys.entity.LogEntity;
import com.tal.live.moudels.sys.service.LogService;
import com.tal.live.pojo.model.RestResult;
import com.tal.live.pojo.model.RestResultBuilder;
import com.tal.live.utils.web.RequestHolder;
import com.tal.live.utils.web.RequestUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.util.http.RequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <Description>
 *
 * @author xianliru@100tal.com
 * @version 1.0
 * @createDate 2019/10/14 13:47
 */
@RestController
@RequestMapping("log")
@Api( value = "log操作",description="log 控制器")
public class LogController {

    @Autowired
    private LogService logService;

    @GetMapping("add")
    @ApiOperation(value = "添加日志", notes = "为测试添加日志数据")
    @ApiVersion(group = {ApiVersionConstant.TEST_V1})
    public RestResult add(){
        LogEntity logEntity = new LogEntity();
        logEntity.setCreateDate(new Date());
        logEntity.setIp(RequestUtils.getIpAddr());
        logEntity.setMethod("test");
        logEntity.setTime(100L);
        logEntity.setUsername("tom");
        logService.save(logEntity);
        return RestResultBuilder.builder().success().build();
    }

    @GetMapping("info/{id}")
    @ApiOperation(value = "获取单个日志信息", notes = "根据用户id查询")
    @ApiVersion(group = {ApiVersionConstant.TEST_V1})
    public RestResult info(@PathVariable Long id){
        LogEntity logEntity = logService.getById(id);
        return RestResultBuilder.builder().success(logEntity).build();
    }

    @ApiOperation(value="获取日志集合记录", notes="根据用户信息查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "username", value = "用户名", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "method", value = "方法名", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "time", value = "执行时间", required = true, dataType = "Long")
    })
    @ApiVersion(group = {ApiVersionConstant.TEST_V1})
    @RequestMapping(value = "/get",method = RequestMethod.GET)
    public RestResult updatePassword(@RequestParam(value="username") String username, @RequestParam(value="method") String method,
                                 @RequestParam(value="time") Long time){

        QueryWrapper<LogEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username).eq("method",method).eq("time",time);
        List<LogEntity> list = logService.list(wrapper);
        return RestResultBuilder.builder().success(list).build();
    }
}
